package br.ufal.tci.nexos.arcolive;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ArCoLIVEServerProperties {
	private Properties properties;
	private static ArCoLIVEServerProperties singleton = null;
	
	private ArCoLIVEServerProperties() {
		this.properties = new Properties();
		this.loadProperties();
	}
	
	public static ArCoLIVEServerProperties getInstance() {
		if (singleton == null) {
			singleton = new ArCoLIVEServerProperties();
		}
		return singleton;
	}
	
	public void saveProperties() {
		FileOutputStream propertyFileStream;
		try {
			propertyFileStream = new FileOutputStream("configuration");
			this.properties.store(propertyFileStream, "Configuration File Generated by ArCOLIVE");
			propertyFileStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void loadProperties() {
		FileInputStream propertyFileStream;
		try {
			propertyFileStream = new FileInputStream("configuration");
			this.properties.load(propertyFileStream);
			propertyFileStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void loadDefaultProperties() {
		this.properties.put("server.name", "ArCoLIVE Server");
		this.properties.put("server.maxConnection", "150");
		this.properties.put("server.port", "80");
		this.properties.put("persistense.serverAddress", "localhost");
		this.properties.put("persistense.serverPort", "3306");
		this.properties.put("persistense.serverDriver", "com.mysql");
		this.saveProperties();
	}
	
	public String getStringProperty(String propertyName) {
		try {
			return this.properties.getProperty(propertyName).trim();	
		} catch (Exception e) {
			return "";
		}
				
	}
	
	public int getIntProperty(String propertyName) {
			return Integer.parseInt(this.properties.getProperty(propertyName).trim());			
	}
	
	public void putProperty(String propertyName, String propertyValue) {
		this.properties.put(propertyName, propertyValue);		
	}
	
	public static void main(String[] args) {
		//ServerProperties.getInstance().loadDefaultProperties();
		//ArCoLIVELogRepository.getInstance().log(ServerProperties.getInstance().getProperty("server.name"));
	}
	
}
